﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_CompetePriceSwithNew : System.Web.UI.Page
{
    string ComSwitchPrj = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            ComSwitchPrj = SysFun.IsSQL(Request.QueryString["projectid"]);
            if (Request.Form["Btnopenall"] == "全部开启限价")
            {
                openjj();
            }
            if (Request.Form["Btncloseall"] == "全部关闭限价")
            {
                closejj();
            }
            if (Request.Form["Btnall"] == "关闭限价功能")
            {
                 closeAll();
            }
            SelectDrop();
            showdata();
        }
    }
    private void SelectDrop()
    {
        Db db = new Db();
        string sql = "select top 1 YiJiaSwitch from V_Bargaining where projectid=" + ComSwitchPrj;
        if (ComPanyName.Text.Trim().Length > 0)
        {
            sql += " and (CompanyName_TB Like '%" + ComPanyName.Text.Trim() + "%' or Usercode_TB Like '%" + ComPanyName.Text.Trim() + "%')";
        }
        DataTable ta = db.DataTable(sql);
        if (ta.Rows.Count > 0)
        {
            if (ta.Rows[0].ItemArray[0].ToString() == "1")
                DropDownList1.SelectedIndex = 0;
            if (ta.Rows[0].ItemArray[0].ToString() == "2")
                DropDownList1.SelectedIndex = 1;
            if (ta.Rows[0].ItemArray[0].ToString() == "3")
                DropDownList1.SelectedIndex = 2;
        }
        

    }
    /// <summary>
/// 数据绑定datagrid
/// </summary>
    protected void showdata()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = " V_Bargaining ";                                               //表名，可以多个表关联
        dbPage.SelectFieldGroup = "accountid_tb,companyName_TB,UserCode_TB,submit,projectid,YiJiaSwitch";
        dbPage.Key = "accountid_tb";
        string sql = "";
        if (ComPanyName.Text.Trim().Length > 0)
        {
            sql = "(CompanyName_TB Like '%" + ComPanyName.Text.Trim() + "%' or Usercode_TB Like '%" + ComPanyName.Text.Trim() + "%') and ";
        }
        dbPage.Where = sql + " isbid<>1 and projectid=" + ComSwitchPrj;// +" and YiJiaSwitch = " + DropDownList1.SelectedValue;

        dbPage.SelectField = " accountid_tb,companyName_TB,UserCode_TB,submit,projectid,YiJiaSwitch ";                        //可以不设，默认为“*”

        dbPage.OrderBy = "accountid_tb";                                //排序字段，可以不输入。不输入时以 Key 作为排序字段

        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]); //设置需要查询第几页
        dbPage.PageSize = 30;                                           //设置每页显示多少条记录


        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table border=1 cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='98%'>");
        sb.Append("<tr>");
        sb.Append("<td class='table_title' nowrap>投标人</td>");
        sb.Append("<td class='table_title' nowrap>投标人注册号</td>");
      //  sb.Append("<td  class='table_title' nowrap>限价次数</td>");
        sb.Append("<td  class='table_title' nowrap>限价状态</td>");
        sb.Append("</tr>");

        int intNum = 0, i, j = 0;
        string zhuangtai = "";//zhuangtai 表示审核状态

        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        while (dr.Read())
        {
            intNum++;
            i = SysFun.ToInt(dr["YiJiaSwitch"]);
            if (i == 0)
            {
                zhuangtai = "关";
            }
            if (i == 1)
            {
                zhuangtai = "开";
            }
            if (j == 0)
            {
                sb.Append("<tr class='table_td'>");
                j++;
            }
            else
            {
                sb.Append("<tr class='table_td2'>");
                j--;
            }

            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["CompanyName_TB"]) + "</td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["Usercode_TB"]) + "</td>");
         //   sb.Append("<td align='center'>" + SysFun.ToTrim(dr["YiJiaSwitch"]) + "</td>");
            sb.Append("<td align='center'>" + zhuangtai + "</td>");
            sb.Append("</tr>");
        }
        dr.Close();
        sb.Append("</table>");
        this.DbPageFlip1.DbPage = dbPage;
        Label1.Text = sb.ToString();
        db.Close();
    }
    /// <summary>
    /// 打开限价报价开关
    /// Submit=1 开关开，Submit=0 开关关
    /// Accept_Company=0 未中标，Accept_Company=1 中标
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void openjj()
    {
        string strSql = "";
        Db db = new Db();
        if (DropDownList1.SelectedIndex == 0)//第一次限价公示将报价默认为第一次限价报价
        {
            strSql = "Update V_Bargaining Set YiJiaPackPrice1=spprice,Submit=1,YiJiaSwitch=" + DropDownList1.SelectedValue;
        }
        else
        {
             strSql = "Update V_Bargaining Set YiJiaSwitch=" + DropDownList1.SelectedValue;
        }
        db.Command(strSql);
        Response.Write("<script>window.alert('限价"+DropDownList1.SelectedValue.ToString()+"次报价全部开启！');</script>");
        //strSql = "";
        //strSql = "update Account set Type = '11' where AccountId in (select distinct AccountId_TB from V_Bargaining)";
        //try
        //{
        //    db.Command(strSql);
        //}
        //catch (Exception ex)
        //{
 
        //}
        db.Close();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Project_Bid", strSql, "../Agency/CompetePriceSwitch.aspx");


    }
    /// <summary>
    /// 关闭限价报价开关
    /// Submit=1 开关开，Submit=0 开关关
    /// Accept_Company=0 未中标，Accept_Company=1 中标
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void closejj()
    {
        Db db = new Db();
        string strSql = "";
        db.Command("update Bargaining set YiJiaSwitch=0");
        strSql = "";
        string strUpdate = "";
        string strNew = "";
        switch (DropDownList1.SelectedValue.ToString())
        {
            case "1":
               // strSql = "update V_Bargaining set isbid = '1',SPPrice = YiJiaPackPrice1,SMPrice = YiJiaPackPrice1/Factor where bidid in (select bidid from dbo.V_Bargaining where isnull(yijiapackprice1,0)<isnull(LimitPrice,0) "
          // + " and yijiapackprice1 is not null and yijiapackprice1 > 0)";
                strUpdate = "update V_Bargaining set YiJiaPackPrice2=YiJiaPackPrice1 where isbid<>1 ";
                strSql = "Update Bargaining Set Submit=2 Where Accept_Company=0 ";
                break;
            case "2":
                //strSql = "update V_Bargaining set isbid = '1',SPPrice = YiJiaPackPrice2,SMPrice = YiJiaPackPrice2/Factor where bidid in (select bidid from dbo.V_Bargaining where isnull(yijiapackprice2,0)<isnull(LimitPrice,0) "
         //  + " and yijiapackprice1 is not null and yijiapackprice2 > 0)";
                strUpdate = "update V_Bargaining set YiJiaPackPrice3=YiJiaPackPrice2 where isbid<>1 ";
                strSql = "Update Bargaining Set Submit=3 Where Accept_Company=0 ";
                break;
           // case "3":
           //     strSql = "update V_Bargaining set isbid = '1',SPPrice = YiJiaPackPrice3,SMPrice = YiJiaPackPrice3/Factor where bidid in (select bidid from dbo.V_Bargaining where isnull(yijiapackprice3,0)<isnull(LimitPrice,0) "
           //+ " and yijiapackprice1 is not null and yijiapackprice3 > 0)";
           //     strNew = "Update Bargaining Set YiJiaSwitch=YiJiaSwitch+1 Where Accept_Company=0 ";
           //     break;
            case "3":
                strSql = "update V_Bargaining set  isbid = '1' where isbid=0  and status=5";//Status = '5'没接受专家限价进入重点监控
              
                break;
        }
        db.Command(strSql);
     
        if (strNew != "")
            db.Command(strNew);
        if (strUpdate != "")
            db.Command(strUpdate);
        Response.Write("<script>window.alert('限价" + DropDownList1.SelectedValue.ToString() + "报价全部关闭！');</script>");
        db.Close();

        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Project_Bid", strSql, "../Agency/CompetePriceSwitch.aspx");

    }
    /// <summary>
    /// 关闭限价功能
    /// </summary>
    private void closeAll()
    {
        Db db = new Db();
        string strSql = "";
        strSql = "update V_Bargaining set YiJiaSwitch = '0'";//全部中标        
        try
        {
            db.Command(strSql);
            Response.Write("<script>window.alert('限价页面全部关闭！');</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>window.alert('限价页面关闭异常！');</script>");
            db.Close();
            return;
        }
        db.Close();
        
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

